<template>
	<view class="resultTask">
		<uni-nav-bar
			color="#ffffff"
			background-color="#00aded"
			:statusBar="true"
			title="站台装卸"
			fixed="true"
			left-icon="left"
			left-text="返回"
			@clickLeft="back"
			@clickRight="excuteMakeZcp"
			right-icon="plus"
			right-text="装车皮"
		></uni-nav-bar>
		<view class="resultTask_top" v-if="transOrderNumber == null">
			<uni-section title="点击图形开始扫描" type="line" titleFontSize="20px">
				<uni-icons type="scan" size="80" @click="onScan"></uni-icons>
			</uni-section>
		</view>
		<view class="resultTask_main">
			<uni-section title="运单信息" type="line" title-font-size="20px">
				<template v-slot:right v-if="transOrderNumber != null">
					<view @click="cancelMask" style="font-weight: 500; font-size: 16px; color: red">
						<text>取消装卸</text>
					</view>
				</template>
				<dil-search :columnData="options.columnDataList" @onSearch="onSearch" class="dilSearch" v-if="transOrderNumber == null"></dil-search>
				<dil-card class="dilCard" @emitEvent="emitEvent" v-bind.sync="options"></dil-card>
			</uni-section>
		</view>
		<view class="editActualWeight">
			<uni-popup ref="editActualWeight_popup" type="bottom" border-radius="10px 10px 0 0" background-color="#fff" style="width: 100%">
				<view class="editActualWeight_main" style="margin: 10px">
					<dil-Form formId="792" ref="form" v-model="form_weight" class="dilForm" labelPosition="right" labelWidth="140px"></dil-Form>
				</view>
				<view class="editActualWeight_floot" style="text-align: center">
					<button type="warn" size="mini" style="margin: 10px" @click="closePopup()">取消</button>
					<button type="primary" size="mini" style="margin: 10px" @click="onclickConfirm(form_weight)">提交</button>
				</view>
			</uni-popup>
		</view>
		<view class="editActualWeight">
			<uni-popup ref="editActualZcp_popup" type="bottom" border-radius="10px 10px 0 0" background-color="#fff" style="width: 100%">
				<view class="editActualWeight_main" style="margin: 10px">
					<dil-Form formId="793" ref="form" v-model="form_weightZcp" class="dilForm" labelPosition="right" labelWidth="140px"></dil-Form>
				</view>
				<view class="editActualWeight_floot" style="text-align: center">
					<button type="warn" size="mini" style="margin: 10px" @click="closePopup()">取消</button>
					<button type="primary" size="mini" style="margin: 10px" @click="onclickConfirm(form_weightZcp)">提交</button>
				</view>
			</uni-popup>
		</view>
	</view>
</template>

<script>
import { accDiv } from '@/utils/utils.js';
export default {
	data() {
		return {
			form_weight: {},
			form_weightZcp: {},
			stepList: [],
			addressMap: {},
			resultStartTime: null,
			formValue: {},
			options: {
				requestUrl: '/api/v1/tms/getZtZxShiji?apiId=724',
				requestQuery: {},
				cardSlotList: [
					// {
					// 	label: '装车皮',
					// 	icon: 'bars',
					// 	emitEvent: 'excuteMakeZcp'
					// },
					// {
					// 	label: '卸钢材',
					// 	icon: 'bars',
					// 	emitEvent: 'excuteMakeXgc'
					// }
				],
				columnDataList: [],
				totalNum: 0,
				rowDataList: []
			},
			transOrderNumber: null,
			transOrderId: null
		};
	},
	onShow() {
		uni.$emit('onShow', 1);
		//获取定位
		this.Tool.getAddress({ loading: false }, (data) => {
			this.addressMap = data;
			this.formValue.loadActualAddress = this.addressMap.address;
			this.formValue = { ...this.addressMap, ...this.formValue };
		});
		// 以点进来的时间作为开始时间;
		this.resultStartTime = new Date();
	},
	watch: {
		transOrderNumber(val) {
			if (val) {
				// this.getTransResult();
			}
		}
	},
	computed: {},
	beforeDestory() {
		this.transOrderNumber = null;
		this.transOrderId = null;
	},
	mounted() {
		this.getOrderSectionDebounce = this.Tool.debounce((data) => {
			this.getOrderSection(data);
		}, 150);
		this.onclickConfirmDebounce = this.Tool.debounce((data) => {
			this.onclickConfirm(data);
		}, 200);
		this.getOrderSectionDebounce({});
	},
	methods: {
		excuteMakeZcp() {
			this.form_weightZcp.capacityId = uni.getStorageSync('capacityId');
			this.$refs.editActualZcp_popup.open('center');
		},
		onclickConfirm(data) {
			let that = this;
			uni.showModal({
				cancelText: '取消',
				confirmText: '确认',
				content: `确认是否装卸`,
				editable: false,
				success: (res) => {
					if (res.confirm) {
						that.disposeHandler(data);
					} else if (res.cancel) {
						uni.showToast({
							title: '用户取消操作！',
							icon: 'none',
							duration: 3000
						});
					}
				}
			});
		},
		async disposeHandler(data) {
			const res = await this.$myRequest({
				url: '/api/v1/tms/disposeHandling',
				data: data
			}).catch((err) => {
				console.log(err);
				uni.showToast({
					duration: 2000,
					title: err,
					icon: 'none'
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					duration: 2000,
					icon: 'success',
					title: '操作成功'
				});
				this.getOrderSectionDebounce({});
				this.closePopup();
			} else {
				uni.showToast({
					duration: 2000,
					icon: 'success',
					title: res.data.data
				});
			}
		},
		async getXgcData(
			data = {
				deleted: 5
			}
		) {
			data.businessScene = '生产';
			data.billingType = '计重';
			data.workFlag = '站台装卸';
			data.dzBack = '物流';
			data.transOrderNumber = this.transOrderNumber;
			const res = await this.$myRequest({
				url: '/api/v1/tms/getTransOrderList?apiId=722',
				data: data
			});
			if (res.data.data.list.length > 0) {
				this.form_weight = JSON.parse(JSON.stringify(res.data.data.list[0]));
				this.form_weight.capacityId = uni.getStorageSync('capacityId');
				this.$refs.editActualWeight_popup.open('center');
			} else {
				uni.showToast({
					title: '无装车实绩请核实',
					icon: 'none',
					duration: 2000
				});
			}
		},
		closePopup() {
			this.$refs.editActualWeight_popup.close();
			this.$refs.editActualZcp_popup.close();
		},
		cancelMask() {
			this.transOrderId = null;
			this.transOrderNumber = null;
			this.getOrderSectionDebounce();
		},
		onSearch(item) {
			this.getOrderSectionDebounce(item);
		},
		back() {
			uni.switchTab({
				url: '/pages/index/index'
			});
		},
		async getOrderSection(
			data = {
				deleted: 5
			}
		) {
			data.businessScene = '生产';
			data.billingType = '计重';
			data.workFlag = '站台装卸';
			data.dzBack = '物流';
			data.capacityId = uni.getStorageSync('capacityId');
			data.transOrderNumber = this.transOrderNumber;
			this.options.requestQuery = { ...data };
		},

		emitEvent(eventName, cardItem) {
			this[eventName](cardItem);
		},
		//扫描事件
		onScan() {
			let that = this;
			that.$commonScan(async (data) => {
				let scanResult = data.scanResult;
				if (!scanResult.includes('YSDD') && scanResult.length != '20') {
					uni.showToast({
						title: '识别信息不符合规则,请重试',
						icon: 'none',
						duration: 2000
					});
				} else {
					that.transOrderNumber = scanResult;
					that.getXgcData({
						workFlag: '站台装卸',
						transOrderNumber: that.transOrderNumber
					});
				}
			});
		},
		//调用装卸接口
		async confirmExecute(data) {
			data.transOrderId = this.options.rowDataList[0].transOrderId;
			data.capacityNumber = this.options.rowDataList[0].capacityNumber;
			const res = await this.$myRequest({
				url: '/api/v1/tms/disposeHandling',
				data: data
			}).catch((err) => {
				uni.showToast({
					duration: 2000,
					title: err
				});
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					duration: 2000,
					icon: 'success',
					title: '操作成功'
				});
				this.transOrderNumber = null;
				this.getOrderSectionDebounce({});
			} else {
				uni.showToast({
					duration: 2000,
					icon: 'success',
					title: res.data.data
				});
			}
		}
		//vuex
	}
};
</script>

<style lang="scss" scoped>
.resultTask {
	width: 100vw;
	height: 100%;
	background-color: #f5f5f5;
	display: flex;
	justify-content: center;
	align-items: center;
	flex-direction: column;
	&_top {
		width: 100vw;
		height: 100%;
		.uni-section {
			width: 100vw;
			/deep/.uni-section-content {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
			}
		}
	}
	.resultTask_top_result {
		margin-top: 10rpx;
		.scroll-Y {
			height: 90vh;
		}
	}
	&_main {
		margin-top: 10rpx;
		width: 100vw;
		height: 100%;
		.uni-section {
			width: 100vw;
			.dilSearch {
				width: 100%;
			}
			/deep/.uni-section-content {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
			}
		}
	}
	&_middle {
		margin-top: 10rpx;
		.uni-section {
			width: 100vw;
			/deep/.uni-section-content {
				display: flex;
				justify-content: center;
				align-items: center;
			}
			/deep/.uni-card {
				border-radius: 40rpx;
				.buttonClass {
					margin-top: 20rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}
				.button {
					border-radius: 40rpx;
				}
			}
		}
	}
	&_floot {
		width: 100vw;
		background-color: #fff;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
		border-radius: 20rpx;
	}
	.editActualWeight {
		&_main {
			.tip {
				font-size: 32rpx;
				font-weight: 500;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				text {
					margin-top: 20rpx;
				}
			}
			.importData {
				margin-top: 20rpx;
				font-size: 32rpx;
				font-weight: 700;
				color: #ff0000;
				text {
					padding: 20rpx;
				}
			}
			.dilForm {
				width: 90%;
				margin-left: 20rpx;
				/deep/.uni-forms .uni-forms-item .uni-forms-item__label {
					font-weight: 700;
					color: #000;
					font-size: 32rpx;
				}
				/deep/.uni-forms .uni-forms-item .uni-forms-item__content {
					font-weight: 700;
					color: #000;
					font-size: 32rpx;
				}

				/deep/.uni-forms .uni-forms-item .uni-forms-item__content .uni-input-placeholder {
					font-weight: 700;
					// color: #ff0000;
					font-size: 28rpx;
				}
				/deep/.uni-forms .uni-forms-item .uni-forms-item__content .uni-input-input:disabled {
					font-weight: 700;
					color: #ff0000;
					font-size: 28rpx;
				}
				// display: flex;
				// justify-content: center;
				// align-items: center;
			}
		}
	}
}
</style>
